//
//  PersonalViewController.m
//  meiyinxiang
//
//  Created by apple on 2017/10/17.
//  Copyright © 2017年 hbjApple. All rights reserved.
//个人中心





#import "PersonalViewController.h"
#import "MYYPersonalCell.h"
#import "MYY_Me_Macro.h"
#import "MYYMeHeadView.h"
#import "MYYHeadTopView.h"
#import "MYYChangeInforViewController.h"
#import "MYYMeVisitorViewController.h"
#import "MYY_FansViewController.h"
#import "MYY_walletViewController.h"
#import "MYY_LoginViewController.h"
#import "MYY_User.h"
#import "MYXSettingViewController.h"
#import "userinfoModel.h"
#import "MYXMemberMdel.h"
#import "NoticeModel.h"
#import "MYXCollectionViewController.h"
#import "MYXInvitationCodeViewController.h"
#import "MYXMyBoossViewController.h"
#import "MYXReleaseViewController.h"
#import "MYXCodeViewController.h"
#import "MYXReleaseHistoryController.h"

@interface PersonalViewController ()<UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate,UIAlertViewDelegate>
@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) UIImageView *indicatorView;
@property(nonatomic, strong) UIView *headerView;
@property(nonatomic, strong) MYYMeHeadView *MYYMeHeadVi;
@property(nonatomic, strong) MYYHeadTopView *MYYHeadTopVi;

@property(nonatomic, strong) MYY_User *currentUser;/**< 个人信息 */

// 列表头像
@property (nonatomic, copy)  NSMutableArray *categoryAvatarUrlArray;
// 列表标题
@property (nonatomic, copy)  NSMutableArray *categorytitleArray;
@property (nonatomic, copy)  NSMutableArray *categoryAvatarArray;
// 列表标题
@property (nonatomic, copy)  NSMutableArray *titleArray;
@property(nonatomic, strong) MYXHttpManager *MYXHttpManag;
@property(nonatomic, strong) MYXMemberMdel *MYXMemberMde;
@property(nonatomic, strong) userinfoModel *userinfoMode;
@end

@implementation PersonalViewController

#pragma mark - load


- (NSMutableArray *)categoryAvatarUrlArray {
    if (_categoryAvatarUrlArray == nil) {
        _categoryAvatarUrlArray = [NSMutableArray arrayWithObjects:@"wode_qianbao", @"wode_woshiboss",nil];
    }
    return _categoryAvatarUrlArray;
}

- (NSMutableArray *)categorytitleArray {
    if (_categorytitleArray == nil) {
        _categorytitleArray = [NSMutableArray arrayWithObjects:@"钱包", @"我是Boss", nil];
    }
    return _categorytitleArray;
}

- (NSMutableArray *)categoryAvatarArray {
    if (_categoryAvatarArray == nil) {
        _categoryAvatarArray = [NSMutableArray arrayWithObjects:@"wode_gerenzhongxin",@"wode_yaoqingma", @"wode_shezhi",nil];
    }
    return _categoryAvatarArray;
}

- (NSMutableArray *)titleArray {
    if (_titleArray == nil) {
        _titleArray = [NSMutableArray arrayWithObjects:@"个人中心",@"Boss邀请码", @"设置", nil];
    }
    return _titleArray;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];

    [self initMemberRequest];
    self.currentUser = [MYY_User readUserFromFile];
    [self.tableView reloadData];
   
    
}

#pragma mark - viewDidLoad


- (void)viewDidLoad {
    [super viewDidLoad];
    
    AdjustsScrollViewInsetNever(self, self.tableView);
    if( [[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f ) {
        float statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
        for( UIView *v in [self.view subviews] ) {
            CGRect rect = v.frame;
            rect.origin.y -= statusBarHeight;
            v.frame = rect;
        }
    }
    self.tableView.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
    [self.view addSubview:self.tableView];
    
    self.currentUser = [MYY_User readUserFromFile];
    if (self.MYXHttpManag == nil) {
        self.MYXHttpManag =[[MYXHttpManager alloc]init];
    }
    if (self.MYXMemberMde == nil) {
        self.MYXMemberMde =[[MYXMemberMdel alloc]init];
    }
    if (!self.currentUser.isLogin) {
        [self.tableView reloadData];
    }else{
        
        [self initMemberRequest];
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(postNSNotificati) name:@"info" object:nil];
}

#pragma mark - 通知



-(void)postNSNotificati{
    self.currentUser = [MYY_User readUserFromFile];
    if (self.MYXHttpManag == nil) {
        self.MYXHttpManag =[[MYXHttpManager alloc]init];

    }
    if (self.MYXMemberMde == nil) {
        self.MYXMemberMde =[[MYXMemberMdel alloc]init];
    }
    [self.tableView reloadData];
//    if (!self.currentUser.isLogin) {
//        [self.tableView reloadData];
//    }else{
//        [self initMemberRequest];
//    }
}

#pragma mark - tableview


- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

#pragma mark - tableview datasource and delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 2;
    }
    return 3;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MYYPersonalCell *cell = [tableView dequeueReusableCellWithIdentifier:Mine_CELL_IDENTIFIER];
    if (!cell) {
        cell = [[MYYPersonalCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Mine_CELL_IDENTIFIER];
    }
    if (indexPath.section==0) {
        [cell nameStr:self.categoryAvatarUrlArray[indexPath.row] titiStr:self.categorytitleArray[indexPath.row]];
       
    }else{
         [cell nameStr:self.categoryAvatarArray[indexPath.row] titiStr:self.titleArray[indexPath.row]];
    }
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 51;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
  
    if (!self.currentUser.isLogin){
        [GRWarnAlter warnCustomerWithMessage:@"请先登录" cancleButtonTitle:@"取消" sureBtnTitle:@"好的" delegate:self];
    }else{
//        if (indexPath.section==0&&indexPath.row==0) {
//            MYXCollectionViewController *MYXCollectionVC =[[MYXCollectionViewController alloc]init];
//            MYXCollectionVC.hidesBottomBarWhenPushed = YES;
//            [self.navigationController pushViewController:MYXCollectionVC animated:YES];
//        }else
        if (indexPath.section==0&&indexPath.row==0){
            MYY_walletViewController *MYY_walletVC =[[MYY_walletViewController alloc]init];
            MYY_walletVC.navigationItem.title = @"我的钱包";
            MYY_walletVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:MYY_walletVC animated:YES];
        }else if (indexPath.section==0&&indexPath.row==1){
            MYXMyBoossViewController *MYXMyBoossVC =[[MYXMyBoossViewController alloc]init];
            MYXMyBoossVC.navigationItem.title = @"我是Boss";
            MYXMyBoossVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:MYXMyBoossVC animated:YES];
        }else if (indexPath.section==1&&indexPath.row==0){
            MYYChangeInforViewController *MYYChangeInforVC =[[MYYChangeInforViewController alloc]init];
            MYYChangeInforVC.navigationItem.title = @"个人中心";
            MYYChangeInforVC.MYXMemberMd = self.MYXMemberMde;
            MYYChangeInforVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:MYYChangeInforVC animated:YES];
        }else if (indexPath.section==1&&indexPath.row==1){
            MYXCodeViewController *MYXInvitationCodeVC =[[MYXCodeViewController alloc]init];
            MYXInvitationCodeVC.MYXMemberMd = self.MYXMemberMde;
            //MYXInvitationCodeVC.navigationItem.title = @"Boss邀请码";
            MYXInvitationCodeVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:MYXInvitationCodeVC animated:YES];
        }else if (indexPath.section==1&&indexPath.row==2){
            MYXSettingViewController *MYXSettingVC =[[MYXSettingViewController alloc]init];
            MYXSettingVC.navigationItem.title = @"设置";
            MYXSettingVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:MYXSettingVC animated:YES];
        }
        
    }
   
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headView =[[UIView alloc]init];
    if (section==0) {
        headView.frame = CGRectMake(0, 0, WIDTH, Mine_CELL_top);
        self.MYYMeHeadVi=[[MYYMeHeadView alloc]init];
        self.MYYMeHeadVi.frame = CGRectMake(0, 0, headView.frame.size.width,headView.frame.size.height);
        [headView addSubview:self.MYYMeHeadVi];
    
        self.MYYHeadTopVi=[[MYYHeadTopView alloc]initWithFrame:CGRectMake(10, self.MYYMeHeadVi.frame.size.height-15-70, self.MYYMeHeadVi.frame.size.width-20,70)];
        self.MYYHeadTopVi.clipsToBounds = YES;
        self.MYYHeadTopVi.layer.cornerRadius =5;
        [self.MYYMeHeadVi addSubview:self.MYYHeadTopVi];
        
        WS(weakSelf)
        if (!self.currentUser.isLogin){
            self.MYYMeHeadVi.nameLable.text = @"请点击登录";
            self.MYYMeHeadVi.nameLable.font = MYYFont(20.0f);
            self.MYYMeHeadVi.mobileLable.hidden = YES;
            self.MYYMeHeadVi.categoryLabel.hidden = YES;
             self.MYYMeHeadVi.hidden = YES;
    
        }else{
            self.MYYMeHeadVi.hidden = NO;
            self.MYYMeHeadVi.mobileLable.hidden = NO;
            self.MYYMeHeadVi.categoryLabel.hidden = NO;
            if (self.MYXMemberMde.userinfo.username.length<=0) {
                self.MYYMeHeadVi.nameLable.text =self.MYXMemberMde.userinfo.phone;
            }else{
                self.MYYMeHeadVi.nameLable.text =self.MYXMemberMde.userinfo.username;
            }
            
            self.MYYMeHeadVi.mobileLable.text =self.MYXMemberMde.userinfo.signature;
            self.MYYMeHeadVi.categoryLabel.text =self.MYXMemberMde.userinfo.home;

            if (self.MYXMemberMde.userinfo.image) {
                [self.MYYMeHeadVi.AvraImageView sd_setImageWithURL:[NSURL URLWithString:self.MYXMemberMde.userinfo.image] placeholderImage:[UIImage imageNamed:@"AppIcon"]];
               
            }
            self.MYYHeadTopVi.MYXMemberMde = self.MYXMemberMde;
            self.MYYHeadTopVi.buttonB = ^(NSInteger tag){
                if (tag==2) {
                    MYXReleaseHistoryController *MYYMeVisitorVC =[[MYXReleaseHistoryController alloc]init];
                    MYYMeVisitorVC.navigationItem.title = @"发布历史";
                    MYYMeVisitorVC.hidesBottomBarWhenPushed = YES;
                    [weakSelf.navigationController pushViewController:MYYMeVisitorVC animated:YES];
                }else {
                    MYY_FansViewController *MYY_FansViVC =[[MYY_FansViewController alloc]init];
                    if (tag==0){
                        MYY_FansViVC.navigationItem.title = @"我的粉丝";
                    }else{
                        MYY_FansViVC.navigationItem.title = @"我的关注";
                    }
                    MYY_FansViVC.hidesBottomBarWhenPushed = YES;
                    [weakSelf.navigationController pushViewController:MYY_FansViVC animated:YES];
                }
                
            };
        }
       
        self.MYYMeHeadVi.ziliaoGes = ^{
            if (!weakSelf.currentUser.isLogin){
                
                [GRWarnAlter warnCustomerWithMessage:@"请先登录" cancleButtonTitle:@"取消" sureBtnTitle:@"好的" delegate:weakSelf];
            }else{
                MYXReleaseViewController *MYXReleaseVC =[[MYXReleaseViewController alloc]init];
                MYXReleaseVC.MYXMemberMd = weakSelf.MYXMemberMde;
                MYXReleaseVC.navigationItem.title = @"我的发布";
                MYXReleaseVC.hidesBottomBarWhenPushed = NO;
                [weakSelf.navigationController pushViewController:MYXReleaseVC animated:YES];
            }
        };
        
    }else{
        headView.frame = CGRectMake(0, 0, WIDTH, 0.1);
        headView.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
    }
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return Mine_CELL_top;
    }
    return 0.1;
}
#pragma mark - 我的个人资料
-(void)initMemberRequest
{
    [self.MYXHttpManag userinfo:^(MYXMemberMdel *user) {
        if (user) {
            self.MYXMemberMde =user;
        
            [self.tableView reloadData];
        }
    } failure:^(NSString *errorCode) {
        [self showServerErrorOfFeedList];
    } netDown:^{
        [self showServerErrorOfFeedList];
    }];
    [[MYXHttpManager shared] getNoticesWitResult:^(BOOL success, NSString *msg, NSArray *arrays) {
        if (success && arrays.count > 0) {
            MYXMemberMdel *member = [MYXMemberMdel sharedInstance];
            member.notices = @"";
            NSString *noticeString = @"";
            NSInteger i = 1;
            for (NoticeModel *noticeModel in arrays) {
                NSString *noticeitem = [noticeModel.content stringByReplacingOccurrencesOfString:@"<p>" withString:@""];
                noticeitem = [noticeitem stringByReplacingOccurrencesOfString:@"</p>" withString:@""];
                noticeitem = [noticeitem stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@" "];
                noticeString = [NSString stringWithFormat:@"%ld.%@  ",i,noticeitem];
                member.notices = [NSString stringWithFormat:@"%@%@",member.notices,noticeString];
                i++;
            }
            [member save];
        }
    }];
}

- (void)showNetDownOfFeedList
{
    
    [self showTopMessage:@"当前网络不可用, 请检查网络设置"];
}

- (void)showServerErrorOfFeedList
{
    [self showTopMessage:@"服务异常, 请稍后重试"];
}
//跳入登录界面
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        MYY_LoginViewController *login = [[MYY_LoginViewController alloc] init];
        MXYNavigationViewController *nvc = [[MXYNavigationViewController alloc] initWithRootViewController:login];
        [self presentViewController:nvc animated:YES completion:nil];
    }
    
}



//MYY_walletViewController
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
